<?php

namespace App\DesignPatterns\Strategy;

use App\Models\User;
use App\Models\Recharge;
use App\Models\Message;

/**
 * 法币交易订单已付款流程
 */
class PayHandle implements HandleInterface
{
    public function handle($uid, $rid)
    {
        $user = User::find($uid);
        $rech = Recharge::find($rid);
        //付款订单必须是待支付订单（状态为1）
        if ($rech->status != 1) {
            //订单信息不匹配
            return respF(3004);
        }
        //如果是买入usdt，订单是商家卖出
        if ($rech->type == 1) {
            if ($rech->rech_order->type != 2 || $rech->uid != $uid) {
                //return response()->json(['status' => 0, 'msg' => '订单信息不匹配']);
                return respF(3004);
            }
            //写入消息
            $msg = ['msg_id' => 9, 'uid' => $uid,"arr"=> ['number' => $rech->id]];
            (new Message())->syspublishsmsAll($msg);
        } elseif ($rech->type == 2) {
            //如果是卖出usdt，订单必须是商户收购USDT
            if ($rech->rech_order->type != 1 || $rech->bid != $uid) {
                //return response()->json(['status' => 0, 'msg' => '订单信息不匹配']);
                return respF(3004);
            }
            //写入消息
            $msg = [
                'msg_id' => 27, 
                'uid' => $rech->uid, 
                'message' => [$user->nickname, $rech->order_no]
            ];
            (new Message())->syspublishsmsAll($msg);
        }

        $rech->update(['status' => 2, 'pay_time' => time()]);

        return response()->json(['status' => 1, 'msg' => '提交成功']);
    }
}